#include "HolyWater.h"
#include "Resource.h"

HolyWater::HolyWater(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, bool isLeft) : Object(x, y)
{	 
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[41]->getFileName()), 125, 0);
	p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[42]->getFileName()), 125, 0);

	this->pos_x = x;
	this->pos_y = y;
	this->veloc_y = HOLY_WATER_VY;
	this->accel_y = HOLY_WATER_GRAVITY;

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	time_light = 0;

	if (isLeft == true)
	{
		this->isLeft = true;
		this->veloc_x = -HOLY_WATER_VX;
		p_sprite->setFrame(0,1);
		p_sprite->setIndex(0);
	} 
	else
	{
		this->isLeft = false;
		this->veloc_x = HOLY_WATER_VX;
		p_sprite->setFrame(1,2);	//frame an
		p_sprite->setIndex(1);
	}

	obj_state = IS_ACTIVING;
}

HolyWater::~HolyWater()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void HolyWater::render(int cameraX, int cameraY)
{
	if (obj_state != IS_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}
}

OBJECT_TYPE HolyWater::getObjectType()
{
	return OBJECT_ITEM;
}


void HolyWater::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch(obj_state)
	{
	case IS_IDLE:
		break;

	case IS_EXPLOSION:
		time_light += gameTime;
		if (time_light > 1000)
		{
			changeState(IS_REMOVE);
		}
		break;

	case IS_ACTIVING:
		{
			width = p_sprite->getTexture()->getWidth();
			height = p_sprite->getTexture()->getHeight();
			pos_x_old = pos_x;
			pos_y_old = pos_y;

			pos_x += veloc_x * gameTime;
			veloc_y -= accel_y * gameTime;
			pos_y += veloc_y * gameTime + 1/2.0 * accel_y * gameTime * gameTime;

			Object* obj;
			list<Object*>::iterator i_obj;
			for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
			{
				obj = *i_obj;
				Box box(this->pos_x_old,
					this->pos_y_old - this->height, 
					width, 
					height, 
					veloc_x * gameTime, 
					veloc_y * gameTime + 1/2.0 * accel_y * gameTime * gameTime);

				Box block(obj->getPosX(),
					obj->getPosY() - obj->getHeight(),
					obj->getWidth(),
					obj->getHeight()
					);

				Box boxphase = Collision::GetSweptBroadphaseBox(box);
				if (Collision::AABBCheck(boxphase, block))
				{
					float normalx = 0;
					float normaly = 0;
					float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
					if (0 < collision_time && collision_time < 1)
					{
						if (obj->getObjectType() == OBJECT_LAND)
						{
							pos_y = pos_y_old + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;
							accel_y = 0;
							veloc_y = 0;
							veloc_x = 0;

							changeState(IS_EXPLOSION);
						}
						if (obj->getObjectType() == OBJECT_ENEMY && obj->getObjState() == ES_ACTIVING)
						{
							changeState(IS_EXPLOSION);
							switch (obj->getIDObjType())
							{
							case 13:	//bat boss
							case 18:	//head boss
								{
									int blood = obj->getBlood();
									blood--;
									obj->setBlood(blood);
									if (obj->getBlood() == 0)
										obj->changeState(ES_DIED);
									else
										obj->changeState(ES_PAUSE);
								}
								break;

							default:								
								obj->changeState(ES_DIED);
								break;
							}
							
						}
					}
				}//end if
			}//end for

			pos_x_old = pos_x;
			pos_y_old = pos_y;

		}
		break;

	}
	p_sprite->update(gameTime);

}


void HolyWater::changeState(int state)
{
	obj_state = state;
	switch(state)
	{
	case IS_ACTIVING:		
		veloc_x = HOLY_WATER_VX;
		accel_y = HOLY_WATER_GRAVITY;
		veloc_y = 0;
		break;

	case IS_EXPLOSION:		
		veloc_x = 0;
		accel_y = 0;
		veloc_y = 0;
		time_light = 0;
		p_sprite = p_light;
		break;

	case IS_REMOVE:
		break;
	}
}
